/**
 * @version 2007-5-8
 * @author xalinx at gmail dot com
 */
package org.nanhill.commons.dbutil.handlers;

import java.lang.reflect.ParameterizedType;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.nanhill.commons.dbutil.BasicRowProcessor;
import org.nanhill.commons.dbutil.RowProcessor;

/**
 * @version 2007-5-8
 * @author xalinx at gmail dot com
 * 
 */
public abstract class AutoBeanHandler<T> implements ParameterizedHandler<T> {
	/**
	 * The Class of beans produced by this handler.
	 */
	private Class<T> type = null;

	/**
	 * The RowProcessor implementation to use when converting rows into beans.
	 */
	private RowProcessor convert = BasicRowProcessor.instance();

	@SuppressWarnings("unchecked")
	public AutoBeanHandler() {
		type = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
	}

	public T handle(ResultSet rs, int rowNum) throws SQLException {
		return this.convert.toBean(rs, rowNum, type);
	}

	public List<T> handle(ResultSet rs) throws SQLException {
		return this.convert.toBeanList(rs, type);
	}

}
